package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.shop.bo.GoodsRefundBO;
import com.junzhiit.nengji.shop.dao.GoodsRefundDAO;
import com.junzhiit.nengji.shop.model.GoodsRefund;
import com.junzhiit.nengji.shop.model.enums.GoodsRefundStatus;
import com.junzhiit.nengji.shop.model.enums.Is;

@Repository
public class GoodsRefundDAOImpl extends AbstractBaseDAO<GoodsRefund> implements GoodsRefundDAO{

	@Override
	public List<GoodsRefundBO> findAll(GoodsRefundBO goodsRefundBO, int pageNumber, int maxPageRow) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select gr.*,o.order_number,u.nick_name,u.mobile_phone,");
		sql.append(" g.name 'goods_name',og.sku_content_name,og.number");
		sql.append(" from goods_refund gr");
		sql.append(" inner join orders_goods og on og.id = gr.orders_goods_id");
		sql.append(" inner join orders o on o.id = og.order_id");
		sql.append(" inner join goods g on g.id = og.goods_id");
		sql.append(" inner join user u on u.id = gr.user_id");
		sql.append(" where gr.is_delete = ?");
		params.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(goodsRefundBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			params.setString(goodsRefundBO.getShopId());
		}
		
		if(goodsRefundBO.getStatus() != null){
			sql.append(" and gr.status = ?");
			params.setInt(goodsRefundBO.getStatus());
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getNickName())){
			sql.append(" and u.nick_name like ?");
			params.setString("%"+goodsRefundBO.getNickName()+"%");
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getOrderNumber())){
			sql.append(" and o.order_number like ?");
			params.setString("%"+goodsRefundBO.getOrderNumber()+"%");
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getStartTime())){
			sql.append(" and gr.created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(goodsRefundBO.getStartTime())));
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getEndTime())){
			sql.append(" and gr.created_time <= ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(goodsRefundBO.getEndTime())));
		}
		sql.append(" order by gr.created_time desc");
		
		return super.queryForList(sql.toString(), GoodsRefundBO.class, params);
	}

	@Override
	public int getTotalCount(GoodsRefundBO goodsRefundBO) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select count(gr.id) from goods_refund gr");
		sql.append(" inner join orders_goods og on og.id = gr.orders_goods_id");
		sql.append(" inner join orders o on o.id = og.order_id");
		sql.append(" inner join goods g on g.id = og.goods_id");
		sql.append(" inner join user u on u.id = o.user_id");
		sql.append(" where gr.is_delete = ?");
		params.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(goodsRefundBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			params.setString(goodsRefundBO.getShopId());
		}
		if(goodsRefundBO.getStatus() != null){
			sql.append(" and gr.status = ?");
			params.setInt(goodsRefundBO.getStatus());
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getNickName())){
			sql.append(" and u.nick_name like ?");
			params.setString("%"+goodsRefundBO.getNickName()+"%");
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getOrderNumber())){
			sql.append(" and o.order_number like ?");
			params.setString("%"+goodsRefundBO.getOrderNumber()+"%");
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getStartTime())){
			sql.append(" and gr.created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(goodsRefundBO.getStartTime())));
		}
		if(StringUtils.isNotBlank(goodsRefundBO.getEndTime())){
			sql.append(" and gr.created_time <= ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(goodsRefundBO.getEndTime())));
		}
		sql.append(" order by gr.created_time desc");
		
		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public GoodsRefundBO findById(String ordersGoodsId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select gr.*,o.order_number,u.nick_name,u.mobile_phone,");
		sql.append(" g.name 'goods_name',og.sku_content_name,og.number");
		sql.append(" from goods_refund gr");
		sql.append(" inner join orders_goods og on og.id = gr.orders_goods_id");
		sql.append(" inner join orders o on o.id = og.order_id");
		sql.append(" inner join goods g on g.id = og.goods_id");
		sql.append(" inner join user u on u.id = gr.user_id");
		sql.append(" where gr.is_delete = ? and gr.id = ?");
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(ordersGoodsId);
		return super.query(sql.toString(), GoodsRefundBO.class,params);
	}

	@Override
	public int getApplyTotal(String shopId) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select count(gr.id) from goods_refund gr");
		sql.append(" inner join orders_goods og on gr.orders_goods_id=og.id");
		sql.append(" inner join orders o on og.order_id = o.id and o.shop_id = ?");
		sql.append(" where gr.is_delete = ? and gr.status = ?");
		param.setString(shopId);
		param.setInt(Is.NO.toIntValue());
		param.setInt(GoodsRefundStatus.APPLY.toIntStatus());
		return queryForInt(sql.toString(),param);
	}
}
